package boardgamer.yavalath.client;

import android.content.Context;
import android.content.Intent;
import boardgamer.yavalath.client.game.GameActivity;
import boardgamer.yavalath.client.game.GamePlayersParcel;
import boardgamer.yavalath.client.rule.RuleActivity;
import boardgamer.yavalath.common.YavalathPlayerState;

import java.util.Map;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author garysoed
 */
public class Navigator {

  private static Navigator INSTANCE = null;

  private final Context context;

  public Navigator(Context context) {
    this.context = checkNotNull(context, "context");
  }

  public void startGame(Map<YavalathPlayer, YavalathPlayerState> players) {
    Intent intent = new Intent(context, GameActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.putExtra(GameActivity.ID_PLAYERS, new GamePlayersParcel(players));
    context.startActivity(intent);
  }

  public void showRule() {
    Intent intent = new Intent(context, RuleActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    context.startActivity(intent);
  }

  public static void initialize(Context context) {
    INSTANCE = new Navigator(context);
  }

  public static Navigator instance() {
    checkNotNull(INSTANCE, "Cannot get instance before initializing");
    return INSTANCE;
  }
}
